import { _decorator, Animation, Component, EditBox, find, Label, Node, tween, UIOpacity, Vec3 } from 'cc';
import { Game } from './Game';
import DataManager from './DataManager';
import EventCenter from './EventCenter';
import SoundManager from './SoundManager';
import SdkManager from './minGameSdk/SdkManager';
import SdkCode from './minGameSdk/SdkCode';
import ViewType from './ViewType';
import { GameResult } from './GameResult';
const { ccclass, property } = _decorator;

@ccclass('MainGame')
export class MainGame extends Component {
    @property(Game)
    private mGameCtr:Game=null;
    @property(Node)
    private mDownItems:Node=null;
    @property(Node)
    private mTopNode:Node=null;
    @property(Node)
    private mSettingNode:Node=null;
    @property(Node)
    private mSettingNode2:Node=null;
    @property(Node)
    private mGameViewNode:Node=null;
    @property(Node)
    private mMainViewNode:Node=null;
    @property(Node)
    private mLevelNode:Node=null;
    @property(Node)
    private mLevelDoneNode:Node=null;
    @property(Node)
    private mSpLevelNode:Node=null;
    @property(Node)
    private mRankNode:Node=null;

    private mFanHuiTimes:number=6;
    private mWinTimes:number=0;
    private mRestTimes:number=1;
    private mInGame:boolean=false;
    private mCurrMapIndex:number=0;
    private mIsTwing:boolean=false;
    private mAnimSacle:Vec3=null;
    protected onLoad(): void {
        
    }
    protected onEnable(): void {
        this.mGameViewNode.active=false;
        this.mLevelNode.active=false;
        this.mLevelDoneNode.active=false;
        this.mSpLevelNode.active=false;
        this.mRankNode.active=false;
       this.changeViewSt(false);
        this.freshSoundBtnSt();
        EventCenter.getIns().on(ViewType.game_complete,this.gameComplete,this);
        EventCenter.getIns().on(ViewType.close_view_reult,this.onCloseResultView,this);
        EventCenter.getIns().on(ViewType.show_anim_by_pos,this.playFullAnim,this)
        
    }
    protected onDisable(): void {
        EventCenter.getIns().off(ViewType.game_complete,this.gameComplete,this);
        EventCenter.getIns().off(ViewType.close_view_reult,this.onCloseResultView,this);
        EventCenter.getIns().off(ViewType.show_anim_by_pos,this.playFullAnim,this)
    }

    protected startClick(e=null,param=null){
        if(this.mIsTwing)return;
        let tgate = param?param:DataManager.ins.getCurrGateCfg()
        if(tgate){
            this.mGameCtr.setData(tgate,param?true:false);
            this.mFanHuiTimes=6;
            this.mRestTimes=1;
            this.mAnimSacle=null;
            this.freshItemsNum();
            this.changeViewSt(true);
            this.showLevel();
        }
    }


    protected gmClick(){
        let tstr = find("gm/EditBox",this.mMainViewNode).getComponent(EditBox).string;
        let tindx = parseInt(tstr)-1;
        let tcfg = DataManager.ins.getGateCfg(tindx);
        this.startClick(this.enabled,tcfg);
    }
    
    private mlevelStartPos:Vec3=new Vec3(0,700,0);
    private mlevelStartPos2:Vec3=new Vec3(0,0,0);
    private mlevelEndPos:Vec3=new Vec3(0,-700,0);
    private showLevel(){
        this.mLevelNode.setPosition(this.mlevelStartPos);
        this.mLevelNode.active=true;
        
        find("lvTxt",this.mLevelNode).getComponent(Label).string=this.mGameCtr.data.getLevelStr();
        let top:UIOpacity = this.mLevelNode.getComponent(UIOpacity);
        top.opacity=100;
        let tself=this;
        this.mIsTwing=true;
        tween(this.mLevelNode).to(0.2,{position:this.mlevelStartPos2}).delay(.5).to(0.2,{position:this.mlevelEndPos},{easing:"backIn"}).call(()=>{
            tself.mLevelNode.active=false;
            tself.mIsTwing=false;
        }).start();
        tween(top).to(0.2,{opacity:255}).delay(0.5).to(0.2,{opacity:0}).start();
    }

    
    private gameComplete(){
        
        this.mDownItems.active=false;
        if(!this.mGameCtr.data.isSp){
            DataManager.ins.nextGate();
            SdkManager.ins.setUserCloudStorage("engine","user",{gate:DataManager.ins.user.currGate})
        }
        this.mWinTimes++;
        this.showLevelDone();
    }
    
    private mlevelDoneStartPos:Vec3=new Vec3(0,-700,0);
    private mlevelDoneStartPos2:Vec3=new Vec3(0,0,0);
    private mlevelDoneEndPos:Vec3=new Vec3(0,700,0);
    private showLevelDone(){
        SoundManager.ins.playOneShot("Success");
        let tmoveMode=this.mLevelDoneNode.getChildByName("bg3");
        //find("",tmoveMode).getComponent(Label)
        tmoveMode.setPosition(this.mlevelDoneStartPos);
        this.mLevelDoneNode.active=true;
        // let top:UIOpacity = this.mLevelNode.getComponent(UIOpacity);
        // top.opacity=0;
        let tself=this;
        this.mIsTwing=true;
        tween(tmoveMode).to(0.2,{position:this.mlevelDoneStartPos2},{easing:"backOut"}).delay(.5).to(0.2,{position:this.mlevelDoneEndPos},{easing:"backIn"}).call(()=>{
            tself.mLevelDoneNode.active=false;
            let tanim = this.node.getChildByName("cAnim");
            tanim.getComponent(Animation).play();
            this.scheduleOnce(()=>{tself.showGameResultView();},0.5);
            
        }).start();
        //tween(top).to(0.2,{opacity:255}).delay(0.5).to(0.2,{opacity:0}).start();
    }

    private showGameResultView(){
        SoundManager.ins.playOneShot("Game_Complete");
        this.mGameViewNode.getComponent(GameResult).setData(this.mGameCtr.data.getAward1Num());
        this.mIsTwing=false;
    }

    private showSpView(){
        this.mSpLevelNode.active=true;
    }
    protected onSpViewClick(e,p){
        this.mSpLevelNode.active=false;
        if(p=="a"){
            this.startClick()
        }else if(p=="b"){
            this.startClick(null,DataManager.ins.getRandomSpGateCfg());
        }
    }
    private onCloseResultView(index:number){
        if(index==0)this.changeViewSt(false);
        else if(index==1){
            if(this.mGameCtr.data.id%10==0){
                this.showSpView();
            }else{
                this.startClick();
            }
        }
    }

    private changeViewSt(inGame:boolean){
        this.mInGame=inGame;
        this.mTopNode.active=inGame;
        this.mDownItems.active=inGame;
        this.mMainViewNode.active=!inGame;
        if(this.mInGame){
           this.freshInGameView();
           SoundManager.ins.stop();
        }else{
            this.freshMapView();
            find("btn2/lvLabel",this.mMainViewNode).getComponent(Label).string="第"+(DataManager.ins.user.currGate+1)+"关";
            find("top/bg1/goldTxt",this.mMainViewNode).getComponent(Label).string=DataManager.ins.user.gold.toString();
            SoundManager.ins.play("seaside");
        }
    }

    private freshInGameView(){
        find("bg1/goldTxt",this.mTopNode).getComponent(Label).string=DataManager.ins.user.gold.toString();
        find("title",this.mTopNode).getComponent(Label).string=this.mGameCtr.data.getLevelStr();
    }

    protected useItemClick(e,p){
        if(p=="a"){
            let tself = this;
            if(this.mFanHuiTimes>0){
                this.mGameCtr.fanhui((suess)=>{
                    if(suess==true){
                        this.mFanHuiTimes--;
                        this.freshItemsNum();
                    }
                });
            }else{
                SdkManager.ins.share(null,(code)=>{
                    if(code==SdkCode.DONE){
                        tself.mFanHuiTimes=6;
                        tself.freshItemsNum();
                    }
                });
            }
        }else if(p=="b"){
            let tself = this;
            if(DataManager.ins.user.growNum>0){
                this.mGameCtr.grow((suess)=>{
                    if(suess==true){
                        DataManager.ins.user.addGrowNum(-1);
                        DataManager.ins.saveUserData();
                        tself.freshItemsNum();
                    }
                });
            }else{
                // SdkManager.ins.showVideoAd((code)=>{
                //     if(code==SdkCode.DONE){
                //         DataManager.ins.user.addGrowNum(1);
                //         tself.freshItemsNum();
                //     }
                // });
                SdkManager.ins.share(null,(code)=>{
                    if(code==SdkCode.DONE){
                        DataManager.ins.user.addGrowNum(1);
                        DataManager.ins.saveUserData();
                        tself.freshItemsNum();
                    }
                });
            }
        }
    }

    protected restAllEleClick(){
        if(this.mRestTimes>0){
            this.showLevel();
            this.mGameCtr.restAllEles();
        }else{
            let tself=this;
            SdkManager.ins.share(null,(code)=>{
                if(code==SdkCode.DONE){
                    this.mRestTimes++;
                    tself.freshItemsNum();
                }
            });
        }
    }

    protected showSettingClick(){
        if(this.mInGame){
            this.mSettingNode.active = !this.mSettingNode.active;
        }else{
            this.mSettingNode2.active = !this.mSettingNode2.active;
        }
    }

    protected exitClick(){
        this.hideSetting();
        this.changeViewSt(false);
        this.mGameCtr.clearAll();
        SoundManager.ins.playOneShot("fail");
    }

    protected soundClick(e,p){
        if(p=="a"){
            SoundManager.ins.setMute(true);
            this.freshSoundBtnSt();
        }else if(p=="b"){
            SoundManager.ins.setMute(false,!this.mInGame);
            this.freshSoundBtnSt();
        }
       
    }

    protected onMapBtnClick(e,p){
        if(p=="a"){
            if(this.mCurrMapIndex>0){
                this.mCurrMapIndex--;
                this.freshMapView();
            }
        }else if(p=="b"){
            if(!DataManager.ins.isMaxMap(this.mCurrMapIndex)){
                this.mCurrMapIndex++;
                this.freshMapView();
            }
        }
    }

    protected onOpenRank(){
        this.mRankNode.active=true;
    }

    protected closeRank(){
        this.mRankNode.active=false;
    }

    private hideSetting(){
        this.mSettingNode.active=false;
        this.mSettingNode2.active=false;
    }

    private freshSoundBtnSt(){
        let tnode = this.mSettingNode;
        find("Sound_on",tnode).active=!SoundManager.ins.mute;
        find("Sound_off",tnode).active=SoundManager.ins.mute;
        tnode = this.mSettingNode2;
        find("Sound_on",tnode).active=!SoundManager.ins.mute;
        find("Sound_off",tnode).active=SoundManager.ins.mute;
    }

    private freshItemsNum(){
        find("icon_a/numbg/num",this.mDownItems).getComponent(Label).string=this.mFanHuiTimes.toString();
        find("icon_a/ad",this.mDownItems).active = this.mFanHuiTimes<=0;
        let tnode:Node = find("icon_b/numbg",this.mDownItems);
        tnode.active = DataManager.ins.user.growNum>0;
        if(tnode.active){
            find("num",tnode).getComponent(Label).string=DataManager.ins.user.growNum.toString();
        }
        tnode = find("icon_b/ad",this.mDownItems);
        tnode.active = DataManager.ins.user.growNum<=0;
        find("icon_rest/ad",this.mTopNode).active = this.mRestTimes<=0;
    }

    private freshMapView(){
        let tdata = DataManager.ins.getMapCfg(this.mCurrMapIndex);
        if(tdata)find("mapBtn/mapLabel",this.mMainViewNode).getComponent(Label).string=tdata.name;
        find("mapBtn/btn1",this.mMainViewNode).active=this.mCurrMapIndex>0;
        find("mapBtn/btn2",this.mMainViewNode).active=!DataManager.ins.isMaxMap(this.mCurrMapIndex);

    }

    
    private playFullAnim(posdata){
        // let tv1 = posdata.v1;
        // let tpos = this.mGameCtr.getScenePos(tv1,this.node);
        // if(!this.mAnimSacle){
        //     let tpos2 = this.mGameCtr.getScenePos(posdata.v2,this.node);
        //     let th = tpos2.y-tpos.y;
        //     let ts = th/160;
        //     ts += 0.1;
        //     let tsx = 1.3;
        //     //if(tsx>1.5)tsx=1.5;
        //     this.mAnimSacle.set(tsx,ts,ts);
        // }
        
        // let taNode = this.node.getChildByName("fullAnim");
        // taNode.active=true;
        // tpos.y -= 10;
        // taNode.setPosition(tpos);
        // taNode.scale=this.mAnimSacle;
        // let tanim1=taNode.getChildByName("full1");
        // let tanim2=taNode.getChildByName("full2");
        // let tanim3=taNode.getChildByName("drop");
        // tanim1.getComponent(Animation).play();
        // tanim2.getComponent(Animation).play();
        // tanim3.getComponent(Animation).play();
        // tanim2.getComponent(Animation).once(Animation.EventType.FINISHED,()=>{
        //     taNode.active=false;
        // },this);
    }
}


